package com.zrrd.yunchmall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.sale.entity.HomeAdvertise;
import com.zrrd.yunchmall.sale.entity.HomeNewProduct;
import com.zrrd.yunchmall.sale.service.IHomeAdvertiseService;
import com.zrrd.yunchmall.sale.service.IHomeRecommendSubjectService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * <p>
 * 首页轮播广告表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/home/advertise")
@Api(tags = "营销服务-广告模块")
public class HomeAdvertiseController {
    @Autowired
    private IHomeAdvertiseService productService;

    @GetMapping("/list")
    @ApiOperation("广告列表")
    private ResponseResult list(@RequestParam int pageSize, @RequestParam int pageNum,
                                String name, Integer type, String endTime) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (!StringUtils.isEmpty(endTime)) {
            queryWrapper.le("end_time", endTime);
        }

        return new ResponseResult<>(200, "查询成功!", productService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @PostMapping("/delete")
    @ApiOperation("删除广告")
    private ResponseResult delete(@RequestParam String ids) {
        for (String id : ids.split(",")) {
            productService.removeById(Long.valueOf(id));
        }
        return new ResponseResult<>(200, "删除成功!");
    }

    @PostMapping("/create")
    @ApiOperation("添加广告")
    private ResponseResult create(@RequestBody HomeAdvertise homeAdvertise) {
        return new ResponseResult<>(200, "删除成功!", productService.save(homeAdvertise));
    }
    @GetMapping("/{id}")
    @ApiOperation("编辑广告")
    private ResponseResult selectById(@PathVariable("id") int id) {
        //QueryWrapper queryWrapper=new QueryWrapper<>();
        //queryWrapper.eq("id",id);
        return new ResponseResult<>(200, "查询成功!",productService.getById(id));
    }
    @PostMapping("/update/{id}")
    @ApiOperation("编辑广告")
    private ResponseResult updateById(@RequestBody HomeAdvertise homeAdvertise,@PathVariable int id) {
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return new ResponseResult<>(200, "删除成功!", productService.update(homeAdvertise,updateWrapper));
    }
}
